package dao

import (
	"FeedXmh/app/model"
	"FeedXmh/redis"
	"fmt"
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gtime"
	"github.com/gogf/gf/util/gconv"
)

type t_alarm struct {
}

var T_alarm = t_alarm{}

func (a *t_alarm) GetList(where ...string) (list gdb.List, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("t_alarm")
	if len(where) == 0 {
	} else {
		model = model.Where(where[0])
	}
	res, err := model.All()
	return res.List(), err
}

func (a *t_alarm) Save(data interface{}) (isOk bool, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	_, err = db.Model("t_alarm").Data(data).Save()
	if err != nil {
		return false, err
	} else {
		return true, nil
	}
}

func (a *t_alarm) GetHappenAlarm(farmid string, houseid string, device_type string, device_addr string, alarm_content string) (item gdb.Map, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("t_alarm")
	res, error := model.Where(
		fmt.Sprintf(
			"farmid='%s' and houseid='%s' and device_type='%s' and device_addr='%s' and alarm_content='%s' and status=1 and add_time>'%s' ",
			farmid,
			houseid,
			device_type,
			device_addr,
			alarm_content, gtime.Now().AddDate(0, -6, 0).String())).One()
	if error != nil {
		err = error
	} else {
		if res != nil {
			item = res.Map()
		}

	}

	return item, err
}

func (a *t_alarm) GetNodeHappenAlarm(device_addr string, sn string, alarm_content string) (item gdb.Map, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("t_alarm")
	res, error := model.Where(fmt.Sprintf("device_addr='%s' and sn='%s' and alarm_content='%s' and status=1 and add_time>'%s' ", device_addr, sn, alarm_content, gtime.Now().AddDate(0, -6, 0).String())).One()
	if error != nil {
		err = error
	} else {
		if res != nil {
			item = res.Map()
		}
	}
	return item, err
}

func (a *t_alarm) AddAlarm(farmid interface{}, houseid interface{}, device_type string, device_addr string, sn string, level int, _type string, subtype string, alarm_content string, status int, remark string) (isOk bool, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	data := gdb.Map{
		"farmid":        farmid,
		"houseid":       houseid,
		"device_type":   device_type,
		"device_addr":   device_addr,
		"sn":            sn,
		"level":         level,
		"type":          _type,
		"subtype":       subtype,
		"alarm_content": alarm_content,
		"status":        status,
		"add_time":      gtime.Now().String(),
		"remark":        remark,
	}
	_, err = db.Model("t_alarm").Data(data).Save()
	if err != nil {
		return false, err
	} else {
		return true, nil
	}
}

func (a *t_alarm) CancelAlarmBak(id string, remark string) (isOk bool, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	one, error := db.Model("t_alarm").Where(" id=?", id).One()
	if error != nil {
		return false, error
	} else {
		item := one.Map()
		item["status"] = 0
		item["cancel_time"] = gtime.Now().String()
		//item["remark"] = remark

		_, err = db.Model("t_alarm").Data(item).Save()
		if err != nil {
			return false, err
		} else {
			return true, nil
		}
	}
}

func (a *t_alarm) CancelAlarm(id string, remark string) (isOk bool, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	_, e := db.Exec(fmt.Sprintf(" update t_alarm set status=0,cancel_time='%s' where id=%s; ", gtime.Now().String(), id))
	if e != nil {
		return false, e
	} else {
		return true, nil
	}
}

func (a *t_alarm) CancelAlarmByUser(id string) (isOk bool, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	one, error := db.Model("t_alarm").Where(" id=?", id).One()
	if error != nil {
		return false, error
	} else {
		item := one.Map()

		device_type := gconv.String(item["device_type"])
		alarm_content := gconv.String(item["alarm_content"])
		if device_type == "DMXFGW_NODE" {
			device_addr := gconv.String(item["device_addr"])
			sn := gconv.String(item["sn"])
			if alarm_content != model.ALARM_SWQ_OFFLINE {
				redis.SetGwNodeAlarm(device_addr, sn, "0")
			} else if alarm_content == model.ALARM_SWQ_OFFLINE {
				redis.SetGwNodeAliveVal(device_addr, sn, "0")
			}
		}

		item["status"] = 0
		item["cancel_time"] = gtime.Now().String()

		item["confirm_status"] = 1
		item["confirm_time"] = gtime.Now().String()

		_, err = db.Model("t_alarm").Data(item).Save()
		if err != nil {
			return false, err
		} else {
			return true, nil
		}
	}
	return isOk, err
}

// ConfirmAlarmById 确认告警
func (a *t_alarm) ConfirmAlarmById(id string, confirm_user_name string) (isOk bool, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	one, error := db.Model("t_alarm").Where(" id=?", id).One()
	if error != nil {
		return false, error
	} else {
		item := one.Map()
		item["confirm_status"] = 1
		item["confirm_time"] = gtime.Now().String()
		item["confirm_user_name"] = confirm_user_name

		_, err = db.Model("t_alarm").Data(item).Save()
		if err != nil {
			return false, err
		} else {
			return true, nil
		}
	}
}

func (a *t_alarm) Delete(id string) (isOk bool, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	_, err = db.Model("t_alarm").Delete(fmt.Sprintf("id=%s", id))
	if err != nil {
		return false, err
	} else {
		return true, nil
	}
}
